package com.cash.app.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.net.toUri
import com.cash.app.BuildConfig
import com.cash.app.R

object ThirdAppUtils {

    private val GP_URL = "https://play.google.com/store/apps/details?id="

    fun openInSystemBrowser(context: Context, url: String) {
        try {
            val intent = Intent(Intent.ACTION_VIEW, url.toUri())
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun gotoGPInBrowser(context: Context) {
        openInSystemBrowser(
            context, "$GP_URL${BuildConfig.APPLICATION_ID}"
        )
    }

    @SuppressLint("QueryPermissionsNeeded")
    fun gotoGP(context: Context) {
        val marketIntent = Intent(Intent.ACTION_VIEW).apply {
            data = "market://details?id=${BuildConfig.APPLICATION_ID}".toUri()
        }

        marketIntent.resolveActivity(context.packageManager)?.let {
            context.startActivity(marketIntent)
        } ?: run {
            marketIntent.data = "$GP_URL${BuildConfig.APPLICATION_ID}".toUri()
            marketIntent.resolveActivity(context.packageManager)?.let {
                context.startActivity(marketIntent)
            } ?: Toast.makeText(
                context, R.string.gp_not_found, Toast.LENGTH_SHORT
            ).show()
        }
    }
} 